package com.hollycrm.unicom.lockservice.memory;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.hollycrm.springframework.factory.DefaultBeanFactory;
import com.hollycrm.unicom.lockservice.LockObject;

/**
 * CleanMemoryLockJob
*
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author Tian Kai
 * @version 1.0
 */
public class CleanMemoryLockJob {

    /**
     * executeInternal
     *
     */
    public void execute() {
        MemoryLockManager lockManager = (MemoryLockManager) DefaultBeanFactory.
            getBean("lockManager");
        Set keySet = lockManager.memoryLocks.keySet();
        List timeoutKeyList =  new ArrayList();
        for (Iterator iter = keySet.iterator(); iter.hasNext();) {
            Object key = iter.next();
            LockObject lock = (LockObject) lockManager.memoryLocks.get(key);
            if (!lock.isValid()) {
                timeoutKeyList.add(key);
            }
        }
        for (Iterator iter = timeoutKeyList.iterator(); iter.hasNext();) {
            lockManager.memoryLocks.remove(iter.next());
        }
    }
}
